package cn.parllay.dingdangproject.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.PixelFormat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.List;

import cn.parllay.dingdangproject.R;
import cn.parllay.dingdangproject.activity.CustomerInfoActivity;
import cn.parllay.dingdangproject.adapter.NoticeAdapter;
import cn.parllay.dingdangproject.bean.notice.Notice;


/**
 * Created by sunny on 2017/8/17.
 */
public class BottomWindow {

    private final static int ANIM_DURATION = 600;
    public static int viewWidth;
    public static int viewHeight;

    private Context mContext;
    private View mBottomWindow;
    private WindowManager wm;
    private LinearLayout linearLayout;
    private ImageView bottom_window_close;
    private float downX;
    private float downY;


    private RecyclerView notice_recyclerView;
    private boolean isShow = false;
    private boolean isClickable = true;
    private NoticeAdapter noticeAdapter;
    private List<Notice> mNotices;

    public BottomWindow(Context context) {
        mContext = context.getApplicationContext();
    }

    public synchronized void popupBottomWindow(List<Notice> notices) {
        mNotices = notices;

        if (!isShow) {
            setUpBottomWindow();

            addToWindowManager();

            setBottomWindowInAnim();

            initEvent();

        }
        bindData(notices);

    }


    private void setUpBottomWindow() {
        wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        //为mHeaderToastView添加parent使其能够展示动画效果
        linearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(llParams);
        mBottomWindow = View.inflate(mContext, R.layout.window_bottom_vip_notice, linearLayout);
        bottom_window_close = (ImageView) mBottomWindow.findViewById(R.id.bottom_window_close);
        notice_recyclerView = (RecyclerView) mBottomWindow.findViewById(R.id.notice_recyclerView);
        notice_recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        noticeAdapter = new NoticeAdapter(mContext);
        notice_recyclerView.setAdapter(noticeAdapter);
    }

    private void addToWindowManager() {

//        wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
//        //为mHeaderToastView添加parent使其能够展示动画效果
//        linearLayout = new LinearLayout(mContext);
//        LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        linearLayout.setLayoutParams(llParams);

        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        wmParams.gravity = Gravity.CENTER | Gravity.RIGHT;
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.format = PixelFormat.TRANSLUCENT;
        wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;
//        linearLayout.addView(mBottomWindow);
        wm.addView(linearLayout, wmParams);
        isShow = true;
    }

    private void initEvent() {

        //为mHeaderToastView添加滑动删除事件
        noticeAdapter.setOnItemClickListener(new NoticeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Notice notice) {

                if (!MyWindowManager1.getInstance().getCenterWindow().isShow()) {
                    noticeAdapter.remove(notice);
                    //进入用户详细信息页
                    startToVipDetailActivity(notice);
                }
            }
        });
        bottom_window_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MyWindowManager1.getInstance().getBottomWindow().dismiss();
            }
        });

    }

    //跳转到顾客详细信息页
    private void startToVipDetailActivity(Notice notice) {

        CustomerInfoActivity.actionStart(mContext, notice.getContactId());

    }

    private void bindData(List<Notice> notices) {
//        bottom_window_close.setVisibility(View.VISIBLE);
        noticeAdapter.refresh(notices);
    }

    /**
     * 为mHeaderToastView添加进入动画
     */
    private void setBottomWindowInAnim() {
        ObjectAnimator a = ObjectAnimator.ofFloat(mBottomWindow, "translationY", 700, 0);
        a.setDuration(ANIM_DURATION);
        a.start();
    }


    /**
     * HeaderToast消失动画
     */
    private void animDismiss() {

        if (null == linearLayout || null == linearLayout.getParent()) {
            //如果linearLayout已经被从wm中移除，直接return
            return;
        }

        ObjectAnimator a = ObjectAnimator.ofFloat(mBottomWindow, "translationY", 0, 700);
        a.setDuration(ANIM_DURATION);
        a.start();

        a.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                dismiss();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    /**
     * 移除弹窗
     */
    public void dismiss() {
        if (null != linearLayout && null != linearLayout.getParent()) {
            wm.removeView(linearLayout);
            isShow = false;
        }
    }

    public boolean isShow() {
        return isShow;
    }

    public boolean isClickable() {
        return isClickable;
    }

    public void setClickable(boolean clickable) {
        isClickable = clickable;
    }

}
